﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data.Entity.SqlServer;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebServiceTMS.Helpers;

namespace WebServiceTMS.Controllers
{
    public class GoalController : ApiController
    {
        //POST api/Goal/AddNewGoal
        [HttpPost]
        [ActionName("AddNewGoal")]
        public HttpResponseMessage AddNewGoal(HttpRequestMessage request)
        {
            try
            {
                var tmp = StringGenerator.GetDictionary(request);

                if (tmp.Count > 0)
                {
                    string name = null;
                    bool isSuccessName = tmp.TryGetValue("Name", out name);
                    string userId = null;
                    bool isSuccessUserId= tmp.TryGetValue("UserId", out userId);
                    userId = TMSCryptography.Decrypt(userId);
                    if (isSuccessName && isSuccessUserId)
                    {
                        int userIdNumeric;
                        bool isUserIdANumber = int.TryParse(userId, out userIdNumeric);
                        if (isUserIdANumber)
                        {
                            return GoalHelper.AddNewGoalHelper(name, userIdNumeric);
                        }
                        return HttpResponseHelper.CreateHttpResponse("error", "Parameter error");
                    }
                    return HttpResponseHelper.CreateHttpResponse("error", "Some parameters are empty!");
                }
                return HttpResponseHelper.CreateHttpResponse("error", "Parameters list is empty");
            }
            catch(Exception ex)
            {
                return HttpResponseHelper.CreateHttpResponse("server_error", ex.Message);
            }
        }

        //POST api/Goal/RemoveGoal
        [HttpPost]
        [ActionName("RemoveGoal")]
        public HttpResponseMessage RemoveGoal(HttpRequestMessage request)
        {
            try
            {
                var tmp = StringGenerator.GetDictionary(request);

                if (tmp.Count > 0)
                {
                    string goalId = null;
                    bool isSuccessGoalId = tmp.TryGetValue("GoalId", out goalId);
                    string userId = null;
                    bool isSuccessUserId = tmp.TryGetValue("UserId", out userId);
                    goalId = TMSCryptography.Decrypt(goalId);
                    userId = TMSCryptography.Decrypt(userId);
                    if (isSuccessGoalId && isSuccessUserId)
                    {
                        int goalIdNumeric;
                        bool isGoalIdANumber = int.TryParse(goalId, out goalIdNumeric);
                        int userIdNumeric;
                        bool isUserIdANumber = int.TryParse(userId, out userIdNumeric);
                        if (isUserIdANumber && isGoalIdANumber)
                        {
                            return GoalHelper.RemoveGoalHelper(goalIdNumeric, userIdNumeric);
                        }
                        return HttpResponseHelper.CreateHttpResponse("error", "Parameter error");
                    }
                    return HttpResponseHelper.CreateHttpResponse("error", "Some parameters are empty!");
                }
                return HttpResponseHelper.CreateHttpResponse("error", "Parameters list is empty");
            }
            catch (Exception ex)
            {
                return HttpResponseHelper.CreateHttpResponse("server_error", ex.Message);
            }
        }

         //GET api/Goals/GetAllUserGoals
        [HttpGet]
        [ActionName("GetAllUserGoals")]
        public HttpResponseMessage GetAllUserGoals(HttpRequestMessage request)
        {
            try
            {
                var tmp = StringGenerator.GetDictionary(request);

                if (tmp.Count > 0)
                {
                    string userId = null;
                    bool isSuccessUserId = tmp.TryGetValue("UserId", out userId);
                    userId = TMSCryptography.Decrypt(userId);
                    if (isSuccessUserId)
                    {
                        int userIdNumeric;
                        bool isUserIdANumber = int.TryParse(userId, out userIdNumeric);
                        if (isUserIdANumber)
                        {
                            return GoalHelper.GetAllUserGoalsHelper(userIdNumeric);
                        }
                        return HttpResponseHelper.CreateHttpResponse("error", "Parameter error");
                    }
                    return HttpResponseHelper.CreateHttpResponse("error", "Some parameters are empty!");
                }
                return HttpResponseHelper.CreateHttpResponse("error", "Parameters list is empty");
            }
            catch (Exception ex)
            {
                return HttpResponseHelper.CreateHttpResponse("server_error", ex.Message);
            }
        }

        //GET api/Goals/GetAllGoals
        [HttpGet]
        [ActionName("GetAllGoals")]
        public HttpResponseMessage GetAllGoals(HttpRequestMessage request)
        {
            try
            {
                using (var context = new TMSEntities())
                {
                    var query = from g in context.Goals
                                select new TMSGoal
                                {
                                    Name = g.Name,
                                    Id = SqlFunctions.StringConvert((double)g.Id).Trim(),
                                    CreatedBy = SqlFunctions.StringConvert((double)g.CreatedBy).Trim()
                                };
                    return GoalHelper.GetJsonGoals(query.ToArray());
                }
            }
            catch (Exception ex)
            {
                return HttpResponseHelper.CreateHttpResponse("server_error", ex.Message);
            }
        }

        //GET api/Goals/EditGoal
        [HttpGet]
        [ActionName("EditGoal")]
        public HttpResponseMessage EditGoal(HttpRequestMessage request)
        {
            try
            {
                var tmp = StringGenerator.GetDictionary(request);

                if (tmp.Count > 0)
                {
                    string goalId = null;
                    bool isSuccessGoalId = tmp.TryGetValue("GoalId", out goalId);
                    string userId = null;
                    bool isSuccessUserId = tmp.TryGetValue("UserId", out userId);
                    string newGoalName = null;
                    bool isSuccessNewGoalName = tmp.TryGetValue("NewGoalName", out newGoalName);
                    goalId = TMSCryptography.Decrypt(goalId);
                    userId = TMSCryptography.Decrypt(userId);
                    if (isSuccessGoalId && isSuccessUserId && isSuccessNewGoalName)
                    {
                        int goalIdNumeric;
                        bool isGoalIdANumber = int.TryParse(goalId, out goalIdNumeric);
                        int userIdNumeric;
                        bool isUserIdANumber = int.TryParse(userId, out userIdNumeric);
                        if (isUserIdANumber && isGoalIdANumber)
                        {
                            return GoalHelper.EditGoalHelper(goalIdNumeric, userIdNumeric, newGoalName);
                        }
                        return HttpResponseHelper.CreateHttpResponse("error", "Parameter error");
                    }
                    return HttpResponseHelper.CreateHttpResponse("error", "Some parameters are empty!");
                }
                return HttpResponseHelper.CreateHttpResponse("error", "Parameters list is empty");
            }
            catch (Exception ex)
            {
                return HttpResponseHelper.CreateHttpResponse("server_error", ex.Message);
            }
        }
    }
}
